package com.qdxwx.data.service.member;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.mapper.member.MemberRecordMapper;
import com.qdxwx.data.mapper.member.MemberRecordOptMapper;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.member.MemberRecordOpt;
import com.qdxwx.models.member.MemberRecordVO;
import com.qdxwx.models.member.MemberSale;
import com.qdxwx.models.order.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * MemberRecord接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class MemberRecordServiceImpl implements MemberRecordService {

	@Autowired
	private MemberRecordMapper memberRecordMapper;
	@Autowired
	private MemberRecordOptMapper memberRecordOptMapper;

	/**
	 * 添加MemberRecord
	 *
	 * @param memberRecord 要添加的MemberRecord
	 * @return id
	 */
	@Override
	public void insert(MemberRecord memberRecord) {
		memberRecordMapper.createMemberRecord(memberRecord);
	}

	@Override
	public void addByOrderDetail(OrderDetail detail, MemberSale sale) {
		/*查询会员状态*/
		MemberRecord memberRecord = getValidByUserId(detail.getUserId(), sale.getTypeId());
		/*如果有开通会员记录则为续费*/
		if (ObjectUtils.isNotNull(memberRecord)) {
			memberRecord.setId(null);
			/*如果是续费开始时间为会员的结束时间*/
			memberRecord.setBeginDate(memberRecord.getEndDate());
			/*到期时间为当前会员的结束时间加结束天数*/
			memberRecord.setEndDate(memberRecord.getEndDate().plusDays(sale.getDays()));
		} else {
			memberRecord = new MemberRecord();
			memberRecord.setBeginDate(LocalDate.now());
			memberRecord.setEndDate(LocalDate.now().plusDays(sale.getDays()));
		}
		memberRecord.setUserId(detail.getUserId());
		memberRecord.setOrderId(detail.getOrderId());
		memberRecord.setPrice(detail.getPrice());
		memberRecord.setSaleId(sale.getId());
		memberRecord.setTypeId(sale.getTypeId());
		memberRecord.setDescription(detail.getDescription());
		memberRecord.setStatus(0);
		insert(memberRecord);
	}

	/**
	 * 修改MemberRecord
	 *
	 * @param memberRecord 要修改的MemberRecord
	 */
	@Override
	public void update(MemberRecord memberRecord) {
		memberRecordMapper.updateMemberRecord(memberRecord);
	}

	/**
	 * 根据id获取单个MemberRecord对象
	 *
	 * @param id 要查询的id
	 * @return MemberRecord
	 */
	@Override
	public MemberRecord get(Long id) {
		return memberRecordMapper.selectMemberRecordById(id);
	}

	/**
	 * 根据用户id、会员类型查询MemberRecord对象(不论是否过期)
	 *
	 * @param userId
	 * @param type
	 * @return
	 */
	@Override
	public MemberRecord getByUserId(Long userId, Integer type) {
		return memberRecordMapper.selectLastMemberRecordByUserId(userId, type);
	}

	/**
	 * 根据用户id、会员类型查询MemberRecord对象(有效期之内)
	 *
	 * @param userId
	 * @param type
	 * @return
	 */
	@Override
	public MemberRecord getValidByUserId(Long userId, Integer type) {
		return memberRecordMapper.selectValidMemberRecordByUserId(userId, type);
	}



	/**
	 * 延期操作
	 *
	 * @param memberRecord
	 * @param manager
	 */
	@Override
	public void updateEndDate(MemberRecord memberRecord, Manager manager) {
		//更新操作 延期
		memberRecordMapper.updateMemberRecord(memberRecord);
		//添加操作记录
		MemberRecordOpt memberRecordOpt = new MemberRecordOpt();
		memberRecordOpt.setUserId(memberRecord.getUserId());//用户id
		memberRecordOpt.setMemberRecordId(memberRecord.getId());//记录id
		memberRecordOpt.setManagerId(manager.getManagerId());//操作人id
		memberRecordOpt.setManagerName(manager.getLoginName());//操作者名称
		memberRecordOpt.setType("delay");//类型
		memberRecordOpt.setDescription("延期操作");
		memberRecordOpt.setCreateTime(LocalDateTime.now());
		memberRecordOptMapper.insertMemberRecordOpt(memberRecordOpt);
	}

	/**
	 * 会员关闭 更新状态
	 *
	 * @param memberRecord
	 */
	@Override
	public void updateStatus(MemberRecord memberRecord) {
		memberRecordMapper.updateMemberStatus(memberRecord);
	}
	/**
	 * 查询用户的全部会员开通记录
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<MemberRecord> getListByUserId(Long userId, PageRequest page) {
		MemberRecord query = new MemberRecord();
		query.setUserId(userId);
		return memberRecordMapper.selectMemberRecordList(query, page);
	}

	@Override
	public List<MemberRecordVO> getVOList(MemberRecord memberRecord, PageRequest page) {
		return memberRecordMapper.selectMemberRecordVOList(memberRecord, page);
	}

	@Override
	public List<MemberRecord> getList(MemberRecord memberRecord, PageRequest page) {
		return memberRecordMapper.selectMemberRecordList(memberRecord, page);
	}

	@Override
	public int count(MemberRecord memberRecord) {
		return memberRecordMapper.selectMemberRecordCount(memberRecord);
	}

	@Override
	public int deleteByIds(String ids) {
		return memberRecordMapper.deleteByIds(TKConvert.toStrArray(ids));
	}

	@Override
	public int delete(long id) {
		return memberRecordMapper.deleteById(id);
	}

	@Override
	public int updateCombine(long userId1, long userId2) {
		if(userId1 < 1 || userId2 < 1){
			throw new RuntimeException("更新会员：参数错误");
		}
		return memberRecordMapper.updateCombine(userId1, userId2);
	}
}